/**
* file sci.h
* author Li Yuan
* platform: mc9s12dp256B
* date: 2012-4-16
* Last modified: 2012.7.22
* version: 1.0.1
* description: SCI(Serial Communication Interface) Support Code
*/
 
#ifndef _SCI_H_
#define _SCI_H_
 
 
 #define SCI0               0
 #define SCI1               1
  
 #define IDLE_LINE          0
 #define ADDRESS_MARK       1
  
 #define NORMAL_MODE        0
 #define LOOP_MODE          1
 #define SING_WIRE_MODE     2
  
 #define RUN_MODE           0
 #define WAIT_MODE          1
  
 #define SCI_TIE            0x80
 #define SCI_TCIE           0x40
 #define SCI_RIE            0x20
 #define SCI_ILIE           0x10
 /* Function Declaration */
  
 /**
 * @brief   initialize the SCI0 module
 */
void SCIInit(void);
 
/**
 * Set the Baud Rate of the SCI.
 * @param  port, port can be SCI0 / SCI1
 * @param  baudRate, the wanted baud rate.
 * @param  busClk, The SCI module clock.
 */
void SCISetBaudRate(unsigned char port, unsigned long baudRate, unsigned long busClk);
 
/**
 * Set the Interrupt Enable Bit
 * @param  port  port can be SCI0 / SCI1
 * @param  ie    Interrupt Enable BIt
 *               SCI_TIE,  Transmitter Interrupt Enable
 *               SCI_TCIE, Transmission Complete Interrupt Enable
 *               SCI_RIE,  Receiver Full Interrupt Enabl
 *               SCI_ILIE, Idle Line Interrupt Enable
 */
void SCISetIEBit (unsigned char port, unsigned char ie);
 
/**
 * Enable The Tx interrupt  (Transmitter Interrupt Enable BIt)
 * @param  port, port can be SCI0 / SCI1
 */
void SCIEnableTxInt(unsigned char port);
 
/**
 * Disable The Tx interrupt  (Transmitter Interrupt Enable BIt)
 * @param  port, port can be SCI0 / SCI1
 */
void SCIDisTxInt(unsigned char port);
 
/**
 * Enable/Disable SCI Receiver
 * @param port  port can be SCI0 / SCI1
 * @param isEnable  0 disable 1 enable
 */
void SCIEnableRecv(unsigned char port, unsigned char isEnable);
 
/**
 * Enable/Disable SCI Transmitter
 * @param port      port can be SCI0 / SCI1
 * @param isEnable  0 disable 1 enable
 */
void SCIEnableTrans(unsigned char port, unsigned char isEnable);
 
/**
 * Set the Idle Line Type
 * @param port  port can be SCI0 / SCI1
 * @param type  0 Idle char bit count begins after start bit
 *              1 Idle char bit count begins after stop bit
 */
void SCISetIdleLineType(unsigned char port, unsigned char type);
 
/**
 * Set the Wakeup Condition.
 * @param port      port can be SCI0 / SCI1
 * @param condi     0 for Idle line wakeup, 1 for address mark wakeup
 */
void SCISetWakeupCondi(unsigned char port, unsigned char condi);
 
/**
 * Enable/Disable parity function and set the Parity type
 * @param port port can be SCI0 / SCI1
 * @param isEnable 0 for disable parity function, 1 for enable
 * @param type 0 for Even Parity, 1 for Odd Parity
 */
void SCISetParity(unsigned char port, unsigned char isEnable, unsigned char type);
 
/**
 * Set the Data Format Mode Bit
 * @param port port can be SCI0 / SCI1
 * @param bits must be 8 or 9
 */
void SCISetDataBit(unsigned char port, unsigned char bits);
 
/**
 * Set the work mode of operation
 * @param port port can be SCI0 / SCI1
 * @param mode mode can be NORMAL_MODE / LOOP_MODE / SING_WIRE_MODE
 */
void SCISetWorkMode(unsigned char port, unsigned char mode);
 
/**
 * Enable/Disable the SCI in wait mode 
 * @param port port can be SCI0 / SCI1
 * @param mode mode can be RUN_MODE / WAIT_MODE
 */
void SCISetPowerMode(unsigned char port, unsigned char mode);
 
/**
 * Set the TXDIR ( Only for Single Wire MODE)
 * @param port port can be SCI0 / SCI1
 * @param dir 0 TXD used as input, 1 TXD used as output
 */
void SCISetTXDIR(unsigned char port, unsigned char dir);
 
/**
 * Send a char throungh SCI module.
 * @param port port can be SCI0 / SCI1
 * @param s the data to be sent
 */
void SCIPutChar( unsigned char port, unsigned char s);
 
/**
 * Send a short int value(Big Endian) throungh SCI module.
 * @param port  port can be SCI0 / SCI1
 * @param i     the data to be sent
 */
void SCIPutShortBigEndian (unsigned char port, short i);
 
/**
 * Send a short int value(Little Endian) throungh SCI module.
 * @param port  port can be SCI0 / SCI1
 * @param i     the data to be sent
 */
void SCIPutShortLittleEndian (unsigned char port, short i);
 
/**
 * Send a long int value throungh SCI module.
 * @param port  port can be SCI0 / SCI1
 * @param i     the data to be sent
 */
void SCIPutLongBigEndian (unsigned char port, long i);
 
/**
 * Send a long int value(Little Endian) throungh SCI module.
 * @param port  port can be SCI0 / SCI1
 * @param i     the data to be sent
 */
void SCIPutLongLittleEndian (unsigned char port, long i);
 
/**
 * Send a char string throungh SCI module.
 * @param port  port can be SCI0 / SCI1
 * @param *str  the string to be sent
 */
void SCIPutStr(unsigned char port, unsigned char *str);
 
/**
 * Send data throungh SCI module.
 * @param port  port can be SCI0 / SCI1
 * @param *p    pointer to the data to be sent
 * @param size  the size(byte) of the data
 */
void SCIWrite(unsigned char port, void *p, int size);
 
/**
 * Send a char string throungh SCI module in a safer manner.
 * @param port  port can be SCI0 / SCI1
 * @param *str  the string to be sent
 * @param maxSize the max size of the string  
 */
void SCIPutStr_safe(unsigned char port, unsigned char *str, int maxSize);
 
/**
 * Receive a char data from SCI module,no reply.
 * @param  port port can be SCI0 / SCI1
 * @return the received char
 */
unsigned char SCIGetChar(unsigned char port);
 
/**
 * Receive a char data from SCI module in a safer manner.
 * @param  port port can be SCI0 / SCI1
 * @param  *p the recived char
 * @param  TimeOut timeout value
 * @return 0 is success, 1 is timeout
 */
unsigned char SCIGetChar_Safe(unsigned char port, unsigned char *p, unsigned short TimeOut) ;
 
 
 
#endif